<template>
  <div class="app-container">

    <!-- 查询和其他操作 -->
    <div class="filter-container">
      <el-form ref="form" label-width="80px">
        <el-upload
          ref="upload"
          :auto-upload="true"
          :multiple="false"
          :show-file-list="false"
          :with-credentials="true"
          :before-upload="upload"
          :action="uploadUrl"
          accept=".xls"
          class="filter-item">
          <el-button slot="trigger" type="primary" icon="el-icon-edit-outline">更新站点库</el-button>
          <div slot="tip" class="el-upload__tip"><span style="font-size: small; color: orangered; ">仅可上传学生购票优惠卡防伪系统中导出的《火车站站名字典.xls》文件!</span>
          </div>
        </el-upload>
      </el-form>
      <hr>
    </div>

    <!-- 查询和其他操作 -->
    <div class="filter-container">
      <el-input v-model="listQuery.province" clearable class="filter-item" style="width: 200px;" placeholder="请输入省份"
                @keyup.enter.native="handleFilter"/>
      <el-input v-model="listQuery.stationName" clearable class="filter-item" style="width: 200px;" placeholder="请输入站点名"
                @keyup.enter.native="handleFilter"/>
      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">查找</el-button>
    </div>

    <!-- 查询结果 -->
    <el-table v-loading="listLoading" :data="list" element-loading-text="正在查询中。。。" row-key="id"
              style="width: 100%;margin-bottom: 20px;" border>
      <el-table-column label="省份" prop="province"/>
      <el-table-column label="站点名" prop="stationName"/>
      <el-table-column label="站名简码" prop="stationShort"/>
      <el-table-column label="站名拼音" prop="stationPinyin"/>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.pageIndex" :limit.sync="listQuery.pageSize"
                @pagination="getList"/>
  </div>
</template>

<script>
import administrationApi from '@/api/administration'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination

export default {
  name: 'Station',
  components: { Pagination },
  data () {
    return {
      list: [],
      total: 0,
      listQuery: {
        pageIndex: 1,
        pageSize: 20,
        province: undefined,
        stationName: undefined
      },
      listLoading: true,
      uploadUrl: '/api/admin/station/upload'
    }
  },
  created () {
    this.getList()
  },
  methods: {
    getList () {
      this.listLoading = true
      administrationApi.listStation(this.listQuery).then(response => {
        this.list = response.response.list
        this.total = response.response.total
        this.listLoading = false
      }).catch(() => {
        this.list = []
        this.total = 0
        this.listLoading = false
      })
    },
    handleFilter () {
      this.listQuery.pageIndex = 1
      this.getList()
    },
    upload (file) {
      return new Promise((resolve, reject) => {
        administrationApi.uploadFile(this.uploadUrl, file).then(response => {
          this.$message.success('更新成功')
          reject(new Error('更新成功'))
        }).catch((error) => {
          console.log(error)
          this.$message.error('更新失败')
          reject(new Error('更新失败'))
        })
      })
    }
  }
}
</script>
